【自動化】NETCONF(ncclient)でCisco機器のコマンド実行・コンフィグ投入

您所在的位置:网站首页 ncclient manager_sessionsend 【自動化】NETCONF(ncclient)でCisco機器のコマンド実行・コンフィグ投入

【自動化】NETCONF(ncclient)でCisco機器のコマンド実行・コンフィグ投入

2024-01-24 20:49| 来源: 网络整理| 查看: 265

NETCONF (ncclient) サンプルコードInterface Listの取得 (get_interface_list.py)Loopback IFの追加 (add_loopback.py)Loopback IFの削除 (delete_loopback.py)XpathによるInterface Listの取得 (get_interface_list_xpath.py) NETCONF (ncclient) サンプルコード

下記リンクのサンプルスクリプトをもとに、ncclientを利用したNETCONFによるコマンド実行・コンフィグ投入・削除のサンプルを作成して実行してみました。

・【Cisco DevNet】Step 3: Walking Through Automating Your Network with NETCONF

Interface Listの取得 (get_interface_list.py) from ncclient import manager import xmltodict import xml.dom.minidom # Create an XML filter for targeted NETCONF queries netconf_filter = """ """ with manager.connect( host="**.**.**.**", port="830", username="*****", password="*****", hostkey_verify=False ) as m: netconf_reply = m.get_config(source = 'running', filter = netconf_filter) print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml()) # Parse the returned XML to an Ordered Dictionary netconf_data = xmltodict.parse(netconf_reply.xml)["rpc-reply"]["data"] # Create a list of interfaces interfaces = netconf_data["interfaces"]["interface"] for interface in interfaces: print("Interface {} enabled status is {}".format( interface["name"], interface["enabled"] ) )

実行結果

$ python3 get_interface_list.py GigabitEthernet1 ianaift:ethernetCsmacd true GigabitEthernet2 to_R4 ianaift:ethernetCsmacd true 172.16.1.1 255.255.255.0 --- snip --- Interface GigabitEthernet1 enabled status is true Interface GigabitEthernet2 enabled status is true --- snip --- Loopback IFの追加 (add_loopback.py) from ncclient import manager import xmltodict import xml.dom.minidom # Create an XML configuration template for ietf-interfaces netconf_interface_template = """ {name} {desc} {type} {status} {ip_address} {mask} """ # Ask for the Interface Details to Add new_loopback = {} new_loopback["name"] = "Loopback" + input("What loopback number to add? ") new_loopback["desc"] = input("What description to use? ") new_loopback["type"] = "ianaift:softwareLoopback" new_loopback["status"] = "true" new_loopback["ip_address"] = input("What IP address? ") new_loopback["mask"] = input("What network mask? ") # Create the NETCONF data payload for this interface netconf_data = netconf_interface_template.format( name = new_loopback["name"], desc = new_loopback["desc"], type = new_loopback["type"], status = new_loopback["status"], ip_address = new_loopback["ip_address"], mask = new_loopback["mask"] ) with manager.connect( host="**.**.**.**", port="830", username="*****", password="*****", hostkey_verify=False ) as m: netconf_reply = m.edit_config(netconf_data, target = 'running')

実行結果

$ python3 add_loopback.py What loopback number to add? 10 What description to use? netconf-test What IP address? 2.2.2.2 What network mask? 255.255.255.255 $ ------- CSR1kv_1#show run int loo 10 Building configuration... Current configuration : 90 bytes ! interface Loopback10 description netconf-test ip address 2.2.2.2 255.255.255.255 end Loopback IFの削除 (delete_loopback.py) from ncclient import manager import xmltodict import xml.dom.minidom # Create an XML configuration template for ietf-interfaces netconf_interface_template = """ {name} """ # Ask for the Interface Details to Delete new_loopback = {} new_loopback["name"] = "Loopback" + input("What loopback number to delete?") # Create the NETCONF data payload for this interface netconf_data = netconf_interface_template.format( name = new_loopback["name"] ) with manager.connect( host="**.**.**.**", port="830", username="*****", password="*****", hostkey_verify=False ) as m: netconf_reply = m.edit_config(netconf_data, target = 'running')

実行結果

$ python3 delete_loopback.py What loopback number to delete?10 $ XpathによるInterface Listの取得 (get_interface_list_xpath.py)

Xpath (XML Path Language)は、XMLをツリー構造としてモデル化し、パス形式でXML内の要素を指定する言語です。ncclientではXpathによる取得データのフィルタも可能です。

from ncclient import manager import xmltodict import xml.dom.minidom from lxml import etree # Create an XML filter for targeted NETCONF queries ele_filter = etree.Element("{urn:ietf:params:xml:ns:netconf:base:1.0}filter", type="xpath", nsmap={None: 'urn:ietf:params:xml:ns:yang:ietf-interfaces'}, select="/interfaces/interface") with manager.connect( host="**.**.**.**", port="830", username="*****", password="*****", hostkey_verify=False ) as m: netconf_reply = m.get_config(source = 'running', filter = ele_filter) # Parse the returned XML to an Ordered Dictionary netconf_data = xmltodict.parse(netconf_reply.xml)["rpc-reply"]["data"] # Create a list of interfaces interfaces = netconf_data["interfaces"][1]["interface"] for interface in interfaces: print("Interface {}".format( interface["name"], ) )

実行結果

$ python3 get_interface_list_xpath.py Interface GigabitEthernet1 Interface GigabitEthernet2 Interface GigabitEthernet3

ネットワーク自動化を基礎から体系的に学びたい方は下記の本がおすすめです。

リンク


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3